package com.lottoad.android.cronos.http;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.util.ByteArrayBuffer;

public class CNHttpResponse {
	public static final int HTTP_STATUS_OK = 200;
	public static final int HTTP_STATUS_FAIL = 0;
	public static final int HTTP_STATUS_BAD_REQUEST = 400;
	public static final int HTTP_STATUS_NOT_FOUND = 404;
	public static final int HTTP_STATUS_INTERNAL_SERVER_ERROR = 500;
	public static final int HTTP_STATUS_SERVICE_UNAVAILABLE = 503;
	public static final int HTTP_STATUS_NO_CONNECTION = 10000;
	public static final int HTTP_STATUS_ROAMING_CAN_NOT_3G = 10001;
	public static final int RESPONSE_NO_EXPIRE_TOKEN = 10002;
	public static final int RESPONSE_NO_SERVER_ERROR = 10003;
	private static volatile CNHttpResponse httpResponse = new CNHttpResponse();

	public static CNHttpResponse getInstance() {
		synchronized (CNHttpManager.class) {
			if (httpResponse == null) {
				httpResponse = new CNHttpResponse();
			}
		}

		return httpResponse;
	}

	public SimpleHttpResponse getResonseResult(HttpResponse response) {
		if (response == null) {
			return null;
		}

		InputStream is = null;
		SimpleHttpResponse localSimpleHttpResponse;
		try {
			is = response.getEntity().getContent();
			byte[] responseBody = readBytesFromInputStream(is);
			is.close();

			localSimpleHttpResponse = new SimpleHttpResponse(response
					.getStatusLine().getStatusCode(), responseBody, response
					.getAllHeaders());
			return localSimpleHttpResponse;
		} catch (Exception e) {
			
			int responseCode = response.getStatusLine().getStatusCode();

			if (responseCode != -1) {
				localSimpleHttpResponse = new SimpleHttpResponse(responseCode,
						response.getEntity().toString());
				return localSimpleHttpResponse;
			}
		} finally {
			try {
				if (is != null)
					is.close();
				is = null;
			} catch (IOException localIOException3) {
			}
		}
		return null;
	}

	public SimpleHttpResponse getResponseNetworkErrorResult() {
		return new SimpleHttpResponse(HTTP_STATUS_NO_CONNECTION, "");
	}

	public SimpleHttpResponse getResponseNetworkRoamingResult() {
		return new SimpleHttpResponse(HTTP_STATUS_ROAMING_CAN_NOT_3G, "");
	}

	public SimpleHttpResponse getResponseServerErrorResult() {
		return new SimpleHttpResponse(RESPONSE_NO_SERVER_ERROR, "");
	}

	private static byte[] readBytesFromInputStream(InputStream is) {
		try {
			ByteArrayBuffer buffer = new ByteArrayBuffer(65536);
			byte[] bytes = new byte[1024];

			boolean firstRead = true;
			int bytesRead;
			while ((bytesRead = is.read(bytes, 0, 1024)) > 0) {

				int startPos = 0;
				int length = bytesRead;
				if (firstRead) {
					if ((bytesRead >= 3) && ((bytes[0] & 0xFF) == 239)
							&& ((bytes[1] & 0xFF) == 187)
							&& ((bytes[2] & 0xFF) == 191)) {
						startPos += 3;
						length -= 3;
					}
					firstRead = false;
				}
				buffer.append(bytes, startPos, length);
			}
			return buffer.toByteArray();
		} catch (Exception localException) {
		}

		return null;
	}

	public class SimpleHttpResponse {
		private int httpStatusCode;
		private byte[] httpResponseBody;
		private Map<String, String> httpHeaders = null;

		@SuppressWarnings({ "rawtypes", "unchecked" })
		public SimpleHttpResponse(int httpStatusCode, byte[] httpResponseBody,
				Header[] headers) {
			this(httpStatusCode, httpResponseBody);
			Header header;
			if (headers != null) {
				this.httpHeaders = new HashMap();
				header = null;
				for (int i = 0; i < headers.length; i++) {
					header = headers[i];
					this.httpHeaders.put(header.getName(), header.getValue());
				}
			}
		}

		public SimpleHttpResponse(int httpStatusCode, byte[] httpResponseBody) {
			this.httpStatusCode = httpStatusCode;
			this.httpResponseBody = httpResponseBody;
		}

		public SimpleHttpResponse(int httpStatusCode, String httpResponseBody) {
			this.httpStatusCode = httpStatusCode;
			this.httpResponseBody = httpResponseBody.getBytes();
		}

		public int getHttpStatusCode() {
			return this.httpStatusCode;
		}

		public byte[] getHttpResponseBody() {
			return this.httpResponseBody;
		}

		public boolean isBodyNull() {
			return this.httpResponseBody == null;
		}

		public String getHttpResponseBodyAsString() {
			if (this.httpResponseBody != null) {
				return new String(this.httpResponseBody);
			}

			return null;
		}

		public ByteArrayInputStream getHttpResponseBodyAsInputStream() {
			if (this.httpResponseBody != null) {
				return new ByteArrayInputStream(this.httpResponseBody);
			}

			return null;
		}

		public Map<String, String> getHeaders() {
			return this.httpHeaders;
		}
	}
}